//
//  PersonalViewController.m
//  Medgretraining
//
//  Created by 辰领科技 on 16/5/17.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PersonalViewController.h"
#import "PersonInfoViewController.h"
#import "MyLoanAttestViewController.h"

@interface PersonalViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIImageView *personHead;//头像
@property (weak, nonatomic) IBOutlet UILabel *personName;//昵称
@property (strong, nonatomic) IBOutlet UILabel *accountIntegral;//积分
@property(nonatomic,strong)NSString * headImage;
@property(nonatomic,strong)NSDictionary *dic;
@end

@implementation PersonalViewController



- (void)viewWillAppear:(BOOL)animated{
    [self changeLanguage];
    [[NSUserDefaults standardUserDefaults]setInteger:4 forKey:@"selectedIndex"];
    if([Common getLoginStatus]){
        [self queryData];
        [self getPersonalInfo];
       _accountIntegral.hidden = NO;
    }else{
        [_personHead setImage:[UIImage imageNamed:@"personal2"]];
        _personName.text = @"点击头像登录";
        _accountIntegral.hidden = YES;
    }
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadLogin) name:@"hadLogin" object:nil];
    self.dic = [NSDictionary dictionary];
    [self initViewLayout];
    [self getPersonalInfo];
//    [self getAccountLeftMoney];
    // Do any additional setup after loading the view.
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.scroView];
     _scroView.frame = CGRectMake(0, 0, 320* VerticalRatio(), 568* VerticalRatio()-49);
     _scroView.contentSize = flexibleSize(CGSizeMake(0, 568* VerticalRatio()-8), NO);
    
    _personHead.layer.masksToBounds = YES;
    _personHead.layer.cornerRadius = 27.5*VerticalRatio();
    

}

#pragma mark == 语言切换
- (void)changeLanguage{
  
}


- (void)hadLogin{
    self.tabBarController.selectedIndex = 0;
}

#pragma mark  ==== 查询用户信息
- (void)getPersonalInfo{
    if (![Common getLoginStatus]) {
       [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryMemberInfoById withHttpType:@"POST" withTag:GS_GS_queryMemberInfoById];
}
#pragma mark ==== 实名认证信息查询
- (void)queryData {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    
    self.shareApi.delegate = self;
//    [self.shareApi  requestOperationWithDic:dic withPath:KQ_certificationMemberInfo withHttpType:@"POST" withTag:KQ_KQ_certificationMemberInfo];
}


#pragma mark - 头像点击事件
- (IBAction)personInfo:(id)sender {
    
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }else{
    
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    PersonInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersonInfo"];
    page.state = [NSString stringWithFormat:@"%@",self.dic[@"muceStatus"]];
    [[Common getNavigation]pushViewController:page];
    }
}



#pragma mark - 供应链认证
- (IBAction)supplyChainAttest:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"SupplyChainAttest"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的招标
- (IBAction)myAdver:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyBids"];
    [[Common getNavigation]pushViewController:page];
}



#pragma mark - 我的收藏
- (IBAction)collection:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyCollect"];
    [[Common getNavigation]pushViewController:page];
}

- (void)showAlertView{
    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"对不起，您无权访问该版块" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
}

#pragma mark - 我的贷款申请
- (IBAction)myAdverAttest:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
     NSInteger userType = [[Common getLoginInfo][@"userType"]integerValue];
    if (userType == 2) {
        UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
        MyLoanAttestViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyLoanAttest"];
        page.userType = userType;
        [[Common getNavigation]pushViewController:page];
    }else{
        [self showAlertView];
    }
    

   
    
}

#pragma mark - 我的贷款受理
- (IBAction)myGteMoney:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    NSInteger userType = [[Common getLoginInfo][@"userType"]integerValue];
    if (userType == 3) {
        UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
        MyLoanAttestViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyLoanAttest"];
        page.userType = userType;
        [[Common getNavigation]pushViewController:page];
    }else{
        [self showAlertView];
    }
}

#pragma mark - 我的订单
- (IBAction)myIndents:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyIndents"];
    [[Common getNavigation]pushViewController:page];
}
#pragma mark - 我的购物车
- (IBAction)myShopCar:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
//
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyShoppingCar"];
    [[Common getNavigation]pushViewController:page];
}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==GS_GS_queryMemberInfoById) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            NSDictionary * dic = database[@"result"];
            NSString * image = [NSString stringWithFormat:@"%@",dic[@"museImage"]];
            _headImage = image;
            if (image && image.length>0 && ![dic[@"museImage"] isEqual:[NSNull null]]) {
                [_personHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
                
            }
            if ([dic[@"museNickName"] isEqual:[NSNull null]]) {
                _personName.text = @"";
            }else{
                _personName.text = [NSString stringWithFormat:@"%@",dic[@"museNickName"]];
            }
            
            if ([dic[@"museScore"] isEqual:[NSNull null]]) {
                _accountIntegral.text = @"我的积分:0分";
            }else{
                _accountIntegral.text = [NSString stringWithFormat:@"我的积分:%@分",dic[@"museScore"]];
            }
            
        }
    }

}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        //
        [self dismissWaitDialog];
        
        
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"hadLogin" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

    

@end
